package com.welkom.everydate.security.implementations;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;  

import com.welkom.everydate.dao.AuthorityDao;
import com.welkom.everydate.dao.RoleDao;
import com.welkom.everydate.dao.UserDao;
import com.welkom.everydate.domain.Role;
import com.welkom.everydate.domain.User;
import com.welkom.everydate.security.interfaces.CustomUserDetailsService;

@Service("customUserDetailsService")  
public class CustomUserDetailsServiceImpl implements CustomUserDetailsService {

	  /** 
     * @Fields serialVersionUID : 
     */  
    private static final long serialVersionUID = 1L;  
      
    @Resource  
    private UserDao userDao;  
    @Resource
    private RoleDao roleDao;
    @Resource
    private AuthorityDao authorityDao;
      
    public UserDetails loadUserByUsername(String username)  throws UsernameNotFoundException {  
        User user = userDao.getUserByUserName(username);  
        List<Role> roles=roleDao.getRolesByUserId(user.getUserId());
        for(Role role:roles){
        	role.setAuthorities(authorityDao.getAuthoritiesByRoleId(role.getRoleId()));
        }
        user.setRoles(roles);
        if (null == user) {
            throw new UsernameNotFoundException("user:" + username + "was not found.");  
        }  
        return user;
    }  

}
